/**********************************************************
    COPYRIGHT
        LGX(c) 2011
    CLASS NAME
        platform_dac.c
    DEPENDENCY

    HISTORY
		03/01/2012, created by Guangxing Liu
**********************************************************/
#include "platform_dac.h"
#include "platform_config.h"
#include "platform_uart.h"
#include "lpc177x_8x_dac.h"
#include "lpc177x_8x_clkpwr.h"

/************************************************************************
	FUNCTION NAME : platform_dac_init
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		Platform dac value init
************************************************************************/
void platform_dac_init(void)
{
	
}

/************************************************************************
	FUNCTION NAME : platform_dac_device_open
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		dac Device Open
************************************************************************/
DeviceHandle platform_dac_device_open(void)
{
	platform_dac_app_call* handle = (platform_dac_app_call*)malloc(sizeof(platform_dac_app_call));
	if(handle)
	{
		handle->iOpt = Platform_Dac_App_Call_None;
	}
	return (DeviceHandle)handle;
 }	

/************************************************************************
	FUNCTION NAME : platform_dac_device_close
	INPUTS: 
		DeviceHandle handle
	OUTPUTS:	
		none
	DESCRIPTION:	
		dac Device Close
************************************************************************/
int platform_dac_device_close(DeviceHandle handle)
{
	platform_dac_app_call* me = (platform_dac_app_call*)handle;
	if(me)
	{
		free(me);
	}
	return 0;
	
}/************************************************************************
	FUNCTION NAME : platform_dac_device_ioctl
	INPUTS: 
		DeviceHandle handle, const char *params
	OUTPUTS:	
		none
	DESCRIPTION:	
		dac Device Control
************************************************************************/
int platform_dac_device_ioctl(DeviceHandle handle, const char *params)
{
	platform_dac_app_call* me = (platform_dac_app_call*)handle;
	
	switch(*params)
	{
		case '0':
			me->iOpt = Platform_Dac_App_Call_Dac_Init;
			return 0;
		case '1':
			me->iOpt = Platform_Dac_App_Call_Dac_Setdacvalue;
			return 0;

		default:
			platform_printf("ERROR--platform_dac_device_ioctl unkown params: %s\r\n",params);
			break;
	}

	return -1;
}

/************************************************************************
	FUNCTION NAME :platform_dac_device_read
	INPUTS: 
		DeviceHandle handle, char *buf, int len
	OUTPUTS:	
		none
	DESCRIPTION:	
		dac Device Read
************************************************************************/
int platform_dac_device_read(DeviceHandle handle, void *buf, int len)
{
	platform_dac_app_call* me = (platform_dac_app_call*)handle;

	
	if(!me || !buf)
		return -1;
	switch(me->iOpt)
	{
		case 0:	
	        return 0;
		default:
			platform_printf("ERROR--platform_dac_device_read unkown params: %s\r\n",me->iOpt);
			break;
	}
	
	return -1;

}

/************************************************************************
	FUNCTION NAME : platform_dac_device_write
	INPUTS: 
		DeviceHandle handle,  const char *buf, int len
	OUTPUTS:	
		none
	DESCRIPTION:	
		dac Device Write
************************************************************************/
int platform_dac_device_write(DeviceHandle handle,  void *buf, int len)
{
	platform_dac_app_call* me = (platform_dac_app_call*)handle;
	if(!me || !buf)
		return -1;
	switch(me->iOpt)
	{
		case Platform_Dac_App_Call_Dac_Init:
			{
				
				DAC_CONVERTER_CFG_Type DAC_ConverterConfigStruct;
				
				platform_dac_config *dac_config = (platform_dac_config *)buf;	

				DAC_ConverterConfigStruct.CNT_ENA = SET;

				DAC_ConverterConfigStruct.DMA_ENA = RESET;
				
				DAC_Init(0);

				DAC_SetDMATimeOut(0, dac_config->rate);

				DAC_ConfigDAConverterControl(0, &DAC_ConverterConfigStruct);
				
			}
			return 0;
		case Platform_Dac_App_Call_Dac_Setdacvalue:
			{
				platform_dac_value*dac_value = (platform_dac_value*)buf;

				DAC_UpdateValue(0, dac_value->value);

				while(!DAC_IsIntRequested(0));
				
			}
			return 0;
		default:
			platform_printf("ERROR--platform_dac_device_write unkown params: %s\r\n",me->iOpt);
			break;
	}
	return -1;

}
/************************************************************************
	FUNCTION NAME : platform_init_dac_device
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		dac Device Init
************************************************************************/
void platform_init_dac_device(void)
{
	DeviceInterface dac = {
                               platform_dac_device_open,
                               platform_dac_device_close,
                               platform_dac_device_ioctl,
                               platform_dac_device_read,
                               platform_dac_device_write
                              };


	hyacinth_install_device(DAC_DEVICE,&dac);
}

/************************************************************************
	FUNCTION NAME : platform_finit_dac_device
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		dac Device Finit
************************************************************************/
void platform_finit_dac_device(void)
{
	hyacinth_uninstall_device(DAC_DEVICE);
}
























